﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Helpers;

namespace FEIStorageSite
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DAL.connectionString = "Data Source=" + Server.MapPath("App_Data\\fei_storage.db");
        }

        public void LoginButtonClick(object sender, EventArgs e)
        {
            try
            {
                loginCaptcha.ValidateCaptcha(captchaTextBox.Text);
                if (loginTextBox.Text == String.Empty)
                {
                    throw new Exception("Please, type your login");
                }
                else if (passwordTextBox.Text == String.Empty)
                {
                    throw new Exception("Please, type your password");
                }
                else if (!loginCaptcha.UserValidated)
                {
                    throw new Exception("Invalid captcha text");
                }
                else
                {
                    if (DAL.Login(loginTextBox.Text, passwordTextBox.Text))
                    {
                        FormsAuthentication.RedirectFromLoginPage(loginTextBox.Text, false);
                    }
                    else
                    {
                        throw new Exception("Invalid credentials");
                    }
                }
            }
            catch (Exception exception)
            {
                loginError.Text = exception.Message;
            }
        }
    }
}
